﻿using System;
using System.Security.Cryptography.X509Certificates;
using eWallet.Security.Cryptography;
using System.Security.Cryptography;

namespace eWallet.Data.Local
{
	public partial class eWalletDataSet
	{
		public partial class GroupsDataTable
		{

			public GroupsRow AddGroupsRow(string name, string description, ICryptoModule cryptoModule)
			{
				return this.AddGroupsRow(name, description, "Rijndael", cryptoModule);
			}

			public GroupsRow AddGroupsRow(string name, string description, string algorithmName, ICryptoModule cryptoModule)
			{
				var keyCipher = cryptoModule.Encrypt<SymmetricAlgorithm>(SymmetricAlgorithm.Create(algorithmName), CryptoModule.SymmetricAlgorithmInputConverter);
				eWalletDataSet ds = (eWalletDataSet)this.DataSet;
				BlobsRow blobRows = ds.Blobs.AddBlobsRow(Guid.NewGuid(), keyCipher);
				return this.AddGroupsRow(name, description, cryptoModule, blobRows);
			}

			public GroupsRow AddGroupsRow(string name, string description, ICryptoModule cryptoModule, BlobsRow encryptedKeyBlob)
			{
				return this.AddGroupsRow(Guid.NewGuid(), DateTime.UtcNow, string.Format(@"{0}\{1}", Environment.UserDomainName, Environment.UserName), name, description, cryptoModule.EncryptionMethod, encryptedKeyBlob, cryptoModule.GetDescription());
			}

			public GroupsRow NewGroupsRow(Guid id, string name, EncryptionMethod encryptionMethod)
			{
				GroupsRow row = this.NewGroupsRow();
				row.Id = id;
				row.Name = name;
				row.EncryptionMethod = encryptionMethod;
				return row;
			}
		}
	}
}
